package com.vrv.netmgr.entry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.adapter.VRVExpandableListAdapter;
import com.vrv.common.util.DataSetUtil;
import com.vrv.common.util.StringUtils;
import com.vrv.netmgr.R;
import com.vrv.netmgr.assets.chart.AssetsChartActivity;
import com.vrv.netmgr.persistent.LoginSharedPreferencesHelper;
import com.vrv.netmgr.vo.LoginInfo;

/**
 * @Description 资产信息导航主界面
 *
 * @author gaopei
 * @date
 */
public class AssetsActivity extends Activity {
	private boolean isBack;

	private Boolean isNavgate = Boolean.TRUE;

	private Boolean isTop = true;

	private LinearLayout leverTwoLayout;

	private ExpandableListAdapter mAdapter;

	private ExpandableListView expandListView;

	private List<Map<String, String>> groups = new ArrayList<Map<String, String>>();

	private List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initViewSetting();
	}

	/**
	 * 加载初始化布局，初始化组件
	 */
	public void initViewSetting() {

		isTop = Boolean.TRUE;

		setContentView(R.layout.assets_nine_cell);
		leverTwoLayout = (LinearLayout) this.findViewById(R.id.lever_2_layout);
		leverTwoLayout.setVisibility(View.VISIBLE);

		MainTab mainTab = (MainTab) getParent();
		mainTab.setTitle(getResources().getString(R.string.assets), false);
		RelativeLayout navLayout = (RelativeLayout) this.findViewById(R.id.assetsChart);

		RelativeLayout nav1Layout = (RelativeLayout) this.findViewById(R.id.navAssetsStatistics);
		nav1Layout.setTag(Boolean.TRUE);
		TextView bubble0 = (TextView) findViewById(R.id.commentWidget_0);
		bubble0.setVisibility(View.GONE);

		RelativeLayout nav2Layout = (RelativeLayout) this.findViewById(R.id.navDeviceQuery);
		nav2Layout.setTag(Boolean.TRUE);
		TextView bubble1 = (TextView) findViewById(R.id.commentWidget_1);
		bubble1.setVisibility(View.GONE);

		RelativeLayout nav3Layout = (RelativeLayout) this.findViewById(R.id.navPatchDistribution);
		nav3Layout.setTag(Boolean.FALSE);
		nav3Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble2 = (TextView) findViewById(R.id.commentWidget_2);
		bubble2.setVisibility(View.GONE);

		RelativeLayout nav4Layout = (RelativeLayout) this.findViewById(R.id.navCascadeCtr);
		nav4Layout.setTag(Boolean.FALSE);
		nav4Layout.setBackgroundColor(getResources().getColor(R.color.gray1));
		TextView bubble3 = (TextView) findViewById(R.id.commentWidget_3);
		bubble3.setVisibility(View.VISIBLE);

		RelativeLayout nav5Layout = (RelativeLayout) this.findViewById(R.id.navNonWindowsDevice);
		nav5Layout.setTag(Boolean.FALSE);
		TextView bubble4 = (TextView) findViewById(R.id.commentWidget_4);
		bubble4.setVisibility(View.VISIBLE);
		nav5Layout.setVisibility(View.INVISIBLE);

		RelativeLayout nav6Layout = (RelativeLayout) this.findViewById(R.id.navTerminalMgr);
		nav6Layout.setTag(Boolean.FALSE);
		TextView bubble5 = (TextView) findViewById(R.id.commentWidget_5);
		bubble5.setVisibility(View.VISIBLE);
		nav6Layout.setVisibility(View.INVISIBLE);

		navLayout.setOnClickListener(new ClickLLListener());
		nav1Layout.setOnClickListener(new ClickLLListener());
		nav2Layout.setOnClickListener(new ClickLLListener());
		nav3Layout.setOnClickListener(new ClickLLListener());
		nav4Layout.setOnClickListener(new ClickLLListener());
		nav5Layout.setOnClickListener(new ClickLLListener());
		nav6Layout.setOnClickListener(new ClickLLListener());
	}

	/**
	 * 切换到第三级布局
	 */
	public void changeToLever3ViewSetting() {
		isTop = Boolean.FALSE;

		setContentView(R.layout.lever_config_nine_cell);// 切换布局
		expandListView = (ExpandableListView) findViewById(R.id.expand_list_view);
		mAdapter = new VRVExpandableListAdapter(AssetsActivity.this, groups, R.layout.groups, childs, R.layout.childs);
		expandListView.setAdapter(mAdapter);
		expandListView.setOnGroupClickListener(new OnGroupClickListener() {
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

				Map<String, String> groupItem = groups.get(groupPosition);
				String activityName = groupItem.get("activityName");

				if (StringUtils.isBlank(activityName))
					return true;

				ComponentName componentName = new ComponentName(AssetsActivity.this, activityName);
				Intent intent = new Intent();
				intent.setComponent(componentName);
				isNavgate = Boolean.FALSE;
				overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
				startActivity(intent);

				return false;
			}
		});
	}

	private class ClickLLListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// 1.初始化数据
			switch (v.getId()) {
			case R.id.assetsChart:
				startActivity(new Intent(AssetsActivity.this, AssetsChartActivity.class));
				return;
			case R.id.navAssetsStatistics:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(0, getResources().getString(R.string.strAssetsStatistics));
				break;
			case R.id.navDeviceQuery:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(1, getResources().getString(R.string.strDeviceQuery));
				break;
			case R.id.navPatchDistribution:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(2, getResources().getString(R.string.strPatchDistribution));
				break;
			case R.id.navCascadeCtr:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(3, getResources().getString(R.string.strCascadeCtr));
				break;
			case R.id.navNonWindowsDevice:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(4, getResources().getString(R.string.strNonWindowsDevice));
				break;
			case R.id.navTerminalMgr:
				if (!Boolean.valueOf(String.valueOf(v.getTag())))
					return;
				fillLever3DataSet(5, getResources().getString(R.string.strTerminalMgr));
				break;

			default:
				break;
			}

			// 2.切换布局填充adapter
			changeToLever3ViewSetting();
		}
	}

	/**
	 * 初始化静态数据
	 *
	 * @param string
	 *
	 * @param i
	 */
	private void fillLever3DataSet(int index, String title) {

		MainTab mainTab = (MainTab) getParent();
		mainTab.setTitle(title, true);
		mainTab.getBackBtn().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
		groups.clear();
		if (index == 0) {
			// 本地资产统计
			Map<String, String> groupMap0 = new HashMap<String, String>();
			groupMap0.put("key", "0");
			groupMap0.put("group", "本地注册情况统计");
			groupMap0.put("activityName", "com.vrv.netmgr.assets.chart.RegisterStatActivity");
			groups.add(groupMap0);

			Map<String, String> groupMap1 = new HashMap<String, String>();
			groupMap1.put("key", "0");
			groupMap1.put("group", "本地设备资源统计");
			groupMap1.put("activityName", "com.vrv.netmgr.assets.chart.DeviceResStatActivity");
			groups.add(groupMap1);

			Map<String, String> groupMap2 = new HashMap<String, String>();
			groupMap2.put("key", "0");
			groupMap2.put("group", "本地设备类型统计");
			groupMap2.put("activityName", "com.vrv.netmgr.assets.chart.DeviceTypeStatActivity");
			groups.add(groupMap2);
		} else if (index == 1) {
			// 设备信息查询
			Map<String, String> groupMap3 = new HashMap<String, String>();
			groupMap3.put("key", "1");
			groupMap3.put("group", "设备信息查询");
			groupMap3.put("activityName", "");
			groups.add(groupMap3);

			Map<String, String> groupMap4 = new HashMap<String, String>();
			groupMap4.put("key", "1");
			groupMap4.put("group", "注册资产查询");
			groupMap4.put("activityName", "com.vrv.netmgr.assets.AssetsQryActivity");
			groups.add(groupMap4);

			Map<String, String> groupMap5 = new HashMap<String, String>();
			groupMap5.put("key", "1");
			groupMap5.put("group", "安装软件查询");
			groupMap5.put("activityName", "com.vrv.netmgr.assets.SoftwareQryActivityDemo");
			groups.add(groupMap5);

			Map<String, String> groupMap6 = new HashMap<String, String>();
			groupMap6.put("key", "1");
			groupMap6.put("group", "软件授权许可查询");
			groupMap6.put("activityName", "");
			groups.add(groupMap6);

			Map<String, String> groupMap7 = new HashMap<String, String>();
			groupMap7.put("key", "1");
			groupMap7.put("group", "首次运行进程查询");
			groupMap7.put("activityName", "");
			groups.add(groupMap7);

			Map<String, String> groupMap8 = new HashMap<String, String>();
			groupMap8.put("key", "1");
			groupMap8.put("group", "首次通信进程查询");
			groupMap8.put("activityName", "");
			groups.add(groupMap8);

			Map<String, String> groupMap9 = new HashMap<String, String>();
			groupMap9.put("key", "1");
			groupMap9.put("group", "首次修改注册表进程查询");
			groupMap9.put("activityName", "");
			groups.add(groupMap9);

			Map<String, String> groupMap10 = new HashMap<String, String>();
			groupMap10.put("key", "1");
			groupMap10.put("group", "共享目录查询");
			groupMap10.put("activityName", "");
			groups.add(groupMap10);

			Map<String, String> groupMap11 = new HashMap<String, String>();
			groupMap11.put("key", "1");
			groupMap11.put("group", "设备IP占用状况查询");
//			groupMap11.put("activityName", "com.vrv.netmgr.assets.DeviceIPOccupyQryActivity");
			/**update by zxb 20130726 暂时关闭*/
			groupMap11.put("activityName", "");
			groups.add(groupMap11);

			Map<String, String> groupMap12 = new HashMap<String, String>();
			groupMap12.put("key", "1");
			groupMap12.put("group", "硬件变化查询");
			groupMap12.put("activityName", "com.vrv.netmgr.assets.HardwareChangedQryActivity");
			groups.add(groupMap12);

			Map<String, String> groupMap13 = new HashMap<String, String>();
			groupMap13.put("key", "1");
			groupMap13.put("group", "软件变化查询");
			groupMap13.put("activityName", "com.vrv.netmgr.assets.SoftwareChangedQryActivity");
			groups.add(groupMap13);

			Map<String, String> groupMap14 = new HashMap<String, String>();
			groupMap14.put("key", "1");
			groupMap14.put("group", "注册日志查询");
			groupMap14.put("activityName", "");
			groups.add(groupMap14);

			Map<String, String> groupMap15 = new HashMap<String, String>();
			groupMap15.put("key", "1");
			groupMap15.put("group", "操作系统安装查询");
			groupMap15.put("activityName", "com.vrv.netmgr.assets.OperatingSysInstallQryActivity");
			groups.add(groupMap15);

			Map<String, String> groupMap16 = new HashMap<String, String>();
			groupMap16.put("key", "1");
			groupMap16.put("group", "计算机开关机查询");
			groupMap16.put("activityName", "");
			groups.add(groupMap16);

			Map<String, String> groupMap17 = new HashMap<String, String>();
			groupMap17.put("key", "1");
			groupMap17.put("group", "离线设备查询");
			groupMap17.put("activityName", "");
			groups.add(groupMap17);

			Map<String, String> groupMap18 = new HashMap<String, String>();
			groupMap18.put("key", "1");
			groupMap18.put("group", "设备黑名单查询");
			groupMap18.put("activityName", "");
			groups.add(groupMap18);

			Map<String, String> groupMap19 = new HashMap<String, String>();
			groupMap19.put("key", "1");
			groupMap19.put("group", "违规接入查询");
			groupMap19.put("activityName", "");
			groups.add(groupMap19);

			Map<String, String> groupMap20 = new HashMap<String, String>();
			groupMap20.put("key", "1");
			groupMap20.put("group", "设备白名单查询");
			groupMap20.put("activityName", "");
			groups.add(groupMap20);

			Map<String, String> groupMap21 = new HashMap<String, String>();
			groupMap21.put("key", "1");
			groupMap21.put("group", "初始化密码查询");
			groupMap21.put("activityName", "");
			groups.add(groupMap21);

			Map<String, String> groupMap22 = new HashMap<String, String>();
			groupMap22.put("key", "1");
			groupMap22.put("group", "备案设备查询");
			groupMap22.put("activityName", "");
			groups.add(groupMap22);
		} else if (index == 2) {
			// 补丁分发
			Map<String, String> groupMap23 = new HashMap<String, String>();
			groupMap23.put("key", "2");
			groupMap23.put("group", "系统运维监视");
			groupMap23.put("activityName", "");
			groups.add(groupMap23);

			Map<String, String> groupMap24 = new HashMap<String, String>();
			groupMap24.put("key", "2");
			groupMap24.put("group", "系统推送设置");
			groupMap24.put("activityName", "");
			groups.add(groupMap24);

			Map<String, String> groupMap25 = new HashMap<String, String>();
			groupMap25.put("key", "2");
			groupMap25.put("group", "软件授权许可证设置");
			groupMap25.put("activityName", "");
			groups.add(groupMap25);

			Map<String, String> groupMap26 = new HashMap<String, String>();
			groupMap26.put("key", "2");
			groupMap26.put("group", "操作系统序列号管理");
			groupMap26.put("activityName", "");
			groups.add(groupMap26);

			Map<String, String> groupMap27 = new HashMap<String, String>();
			groupMap27.put("key", "2");
			groupMap27.put("group", "数据库配置");
			groupMap27.put("activityName", "");
			groups.add(groupMap27);

			Map<String, String> groupMap28 = new HashMap<String, String>();
			groupMap28.put("key", "2");
			groupMap28.put("group", "数据重整");
			groupMap28.put("activityName", "");
			groups.add(groupMap28);

			Map<String, String> groupMap29 = new HashMap<String, String>();
			groupMap29.put("key", "2");
			groupMap29.put("group", "数字证书映射情况");
			groupMap29.put("activityName", "");
			groups.add(groupMap29);

			Map<String, String> groupMap30 = new HashMap<String, String>();
			groupMap30.put("key", "2");
			groupMap30.put("group", "工具上传管理");
			groupMap30.put("activityName", "");
			groups.add(groupMap30);

			Map<String, String> groupMap31 = new HashMap<String, String>();
			groupMap31.put("key", "2");
			groupMap31.put("group", "普通工具下载");
			groupMap31.put("activityName", "");
			groups.add(groupMap31);

			Map<String, String> groupMap32 = new HashMap<String, String>();
			groupMap32.put("key", "2");
			groupMap32.put("group", "数字证书审计上报");
			groupMap32.put("activityName", "");
			groups.add(groupMap32);
		} else if (index == 3) {
			// 级联总控
			Map<String, String> groupMap33 = new HashMap<String, String>();
			groupMap33.put("key", "3");
			groupMap33.put("group", "级联注册情况统计");
			groupMap33.put("activityName", "");
			groups.add(groupMap33);

			Map<String, String> groupMap34 = new HashMap<String, String>();
			groupMap34.put("key", "3");
			groupMap34.put("group", "级联设备资源统计");
			groupMap34.put("activityName", "");
			groups.add(groupMap34);

			Map<String, String> groupMap35 = new HashMap<String, String>();
			groupMap35.put("key", "3");
			groupMap35.put("group", "级联设备类型统计");
			groupMap35.put("activityName", "");
			groups.add(groupMap35);

			Map<String, String> groupMap36 = new HashMap<String, String>();
			groupMap36.put("key", "3");
			groupMap36.put("group", "级联管理控制");
			groupMap36.put("activityName", "");
			groups.add(groupMap36);

			Map<String, String> groupMap37 = new HashMap<String, String>();
			groupMap37.put("key", "3");
			groupMap37.put("group", "区域管理器状态查询");
			groupMap37.put("activityName", "");
			groups.add(groupMap37);

			Map<String, String> groupMap38 = new HashMap<String, String>();
			groupMap38.put("key", "3");
			groupMap38.put("group", "区域扫描器状态查询");
			groupMap38.put("activityName", "");
			groups.add(groupMap38);
		} else if (index == 4) {
			// 非Windows操作系统设备
			Map<String, String> groupMap39 = new HashMap<String, String>();
			groupMap39.put("key", "4");
			groupMap39.put("group", "硬件资产查询");
			groupMap39.put("activityName", "");
			groups.add(groupMap39);

			Map<String, String> groupMap40 = new HashMap<String, String>();
			groupMap40.put("key", "4");
			groupMap40.put("group", "软件资产查询");
			groupMap40.put("activityName", "");
			groups.add(groupMap40);
		} else if (index == 5) {
			// 终端管理
			Map<String, String> groupMap41 = new HashMap<String, String>();
			groupMap41.put("key", "5");
			groupMap41.put("group", "终端点-点控制");
			groupMap41.put("activityName", "");
			groups.add(groupMap41);

			Map<String, String> groupMap42 = new HashMap<String, String>();
			groupMap42.put("key", "5");
			groupMap42.put("group", "Intel vPro点对点管理");
			groupMap42.put("activityName", "");
			groups.add(groupMap42);
		}

		childs.clear();
		for (int i = 0; i < groups.size(); i++) {
			List<Map<String, String>> varList = new ArrayList<Map<String, String>>(0);
			childs.add(varList);
		}
		DataSetUtil.checkDataSource(this.groups, this.childs);
	}

	/**
	 *
	 */
	Handler backHandler = new Handler(){
		public void handleMessage(Message msg){
			isBack = false;
		}
	};
	@Override
	public void onBackPressed() {
		LoginInfo loginInfo = LoginSharedPreferencesHelper.getLoginInfo(getApplicationContext());
		if (!isTop) {
			initViewSetting();
		} else if (!isBack) {
			isBack = true;
			if(loginInfo.isAutoLogin()){
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(getApplicationContext(), "再按一次到登录界面", Toast.LENGTH_SHORT).show();
			}
			backHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			if(loginInfo.isAutoLogin()){
				finish();
				System.exit(0);
			}else{
				finish();
				startActivity(new Intent(AssetsActivity.this, LoginActivity.class));
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isNavgate) {
			initViewSetting();
		} else {
			isNavgate = Boolean.TRUE;
		}
	}

}
